/*
 * 创建日期 2010-4-16
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.html.query;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.bean.ParamContext;
import  org.fantasy.cpp.core.html.QueryHtml;
import  org.fantasy.cpp.core.pojo.QueryConfig;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.pojo.QueryPage;
import  org.fantasy.cpp.core.pojo.QueryType;
import  org.fantasy.cpp.core.service.QueryService;
import  org.fantasy.cpp.core.util.ConvertUtil;
import  org.fantasy.cpp.core.util.ParseUtil;
import  org.fantasy.cpp.core.util.QueryUtil;
import  org.fantasy.cpp.web.controller.QueryController;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.util.RequestUtil;
import  org.fantasy.common.util.StringUtil;
import  org.fantasy.common.util.WebUtil;

/**
 *  抽象条件构造器
 * @author: 王文成
 * @version: 1.0
 * @since 2010-4-16
 */
public abstract class AbstractQueryHtml implements QueryHtml {

	/**
	 * itemID
	 */
	protected String itemId;

	/**
	 * 显示名称
	 */
	protected String name;

	/**
	 * 名称
	 */
	protected String code;

	/**
	 * 设置值
	 */
	protected String value;

	/**
	 * 条件必选
	 */
	protected boolean checked;

	/**
	 * 默认是否选中
	 */
	protected boolean defChecked;

	/**
	 * 默认值
	 */
	protected String defValue;

	/**
	 * 类型名称
	 */
	protected String className;

	/**
	 * 类型名称
	 */
	protected String cssClass;

	/**
	 * 样式定义
	 */
	protected String style;

	/**
	 * 格式定义
	 */
	protected String format;

	/**
	 * 数据源
	 */
	protected String source;

	/**
	 * 数据源
	 */
	protected ParamContext param;

	/**
	 * 当前请求
	 */
	protected HttpServletRequest request;

	/**
	 * 当前请求类型
	 */
	protected String itemType;

	/**
	 * 是否只读
	 */
	protected boolean readonly;

	/**
	 * 是否能为空
	 */
	protected boolean allowNull;

	/**
	 * 是否多选
	 */
	protected String multiple;

	/**
	 * 是否是异步
	 */
	protected boolean async;
	
	/**
	 * 是否自动查询
	 */
	protected boolean autoQuery;

	/**
	 * ContextPath
	 */
	protected String ctx;

	/**
	 * 初始ID
	 */
	protected String startId;
	
	

	public AbstractQueryHtml(HttpServletRequest request, ParamContext param) {
		this.param = param;
		this.ctx = request.getContextPath();
		this.itemId = param.get(ITEM_ID);
		this.code = param.get(CODE);
		this.value = param.get(VALUE);
		this.style = param.get(CSS_STYLE);
		this.className = param.get(CLASS_NAME);
		this.defValue = param.get(DEF_VALUE);
		this.cssClass = param.get(CSS_CLASS);
		this.format = param.get(FORMAT);
		this.allowNull = param.isTrue(AllOW_NULL);
		this.source = param.get(SOURCE);
		this.checked = param.isTrue(CHECKED);
		this.defChecked = param.isTrue(DEF_CHECKED);
		this.request = request;
		this.name = param.get(NAME);
		this.readonly = param.isTrue(READONLY);
		this.multiple = param.get(MULTIPLE);
		this.async = param.isTrue(ASYNC);
		this.startId = param.get(START_ID);
		this.autoQuery = param.isTrue(AUTO_QUERY);
		
	}

	/**
	 * 得到名字
	 * 
	 * @return
	 * @throws Exception
	 */
	protected String getLabelName() {
		StringBuffer html = new StringBuffer(128);
		html.append(name + "：");
		return html.toString();
	}

	/**
	 * 是否只读
	 * 
	 * @return
	 * @throws Exception
	 */
	protected String getIsReadonly(String s) {
		return readonly ? " readonly=\"true\" " : "";
	}

	/**
	 * 取得样式
	 * 
	 * @return
	 */
	protected String getStyleHtml() {
		StringBuffer html = new StringBuffer(32);
		if (StringUtil.isValid(style)) {
			html.append(" style='" + style + "'");
		}
		return html.toString();
	}

	/**
	 * 取得css样式
	 * 
	 * @return
	 */
	protected String getClassHtml() {
		StringBuffer html = new StringBuffer(32);
		if (StringUtil.isValid(cssClass)) {
			html.append(" class='" + cssClass + "'");
		}
		return html.toString();
	}

	/**
	 * 取得multiple
	 * 
	 * @return
	 */
	protected String getMultiple() {
		StringBuffer html = new StringBuffer(32);
		if (!"".equals(multiple)) {
			html.append(" multiple='" + multiple + "'");
		}
		return html.toString();
	}

	/**
	 * 取得数据源
	 * 
	 * @return
	 */
	protected Object getSource() throws Exception {
		// 不是异步方式，执行查询和数据解析。
		if (!async) {
			QueryPage page = (QueryPage) WebUtil.findAttribute(request, QueryController.PAGE_INFO);
			QueryItem item = page.getQueryItem(new Long(itemId));
			QueryConfig config = item.getQueryConfig();
			return QueryUtil.getList(request, config.getQueryId(),getQueryMap(item));
		}
		return new ArrayList<Row>(0);
	}
	
	/**
	 * 取得通用查询Map
	 * @param item
	 * @return
	 * @throws Exception
	 */
	private Map<String, Object> getQueryMap(QueryItem item) throws Exception {
		Map<String,Object> qryMap = RequestUtil.getRequestMap(request);
		String config = item.getAttr(QueryHtml.START_ID);
		String startId = ConvertUtil.covert(request, config);
		qryMap.put(QueryHtml.START_ID, startId);
		return qryMap;
	}

	/**
	 * 特殊字符过滤
	 * 
	 * @return
	 */
	public String charFilter(String input) {
		StringBuilder filtered = new StringBuilder(64);
		char c;
		for (int i = 0; i < input.length(); i++) {
			c = input.charAt(i);
			if (c == '<') {
				filtered.append("&lt;");
			} else if (c == '>') {
				filtered.append("&gt;");
			} else if (c == '\"') {
				filtered.append("&quot;");
			} else if (c == '&') {
				filtered.append("&amp;");
			} else {
				filtered.append(c);
			}
		}
		return (filtered.toString());
	}
	
	/**
	 * 取得组件ID
	 * 
	 * @return
	 */
	protected String getStartId() {
		return code + "Combo";
	}
	
	/**
	 * 取得组件ID
	 * 
	 * @return
	 */
	protected String getAutoQuery() {
		return autoQuery ?  " onchange=\"Action.qry();\"" : " ";
	}

	/**
	 * 取得组件ID
	 * 
	 * @return
	 */
	protected String getComboId() {
		return code + "Combo";
	}
}
